package com.zhangpf.boot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.zhangpf.boot.dao.mongo.StudentMongoDAO;
import com.zhangpf.boot.dao.mongo.TeacherMongoDAO;
import com.zhangpf.boot.entity.Student;
import com.zhangpf.boot.entity.Teacher;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class Service {
	@Autowired
    private StudentMongoDAO stuDao;
    
    @Autowired
    private TeacherMongoDAO teaDao;
	
    @Transactional(rollbackFor = { Exception.class })
	public void testTC() {
    	try {
   		 Teacher t = new Teacher();
   		 t.setName("李老师");
   		 Teacher teacher = teaDao.save(t);
//   		 int i = 1/0;
   		 Student s1 = new Student();
   		 s1.setName("张三");
   		 s1.setAge(20);
   		 s1.setAddr("China");
   		 Student save = stuDao.save(s1);
   		 log.info(JSON.toJSONString(teacher));
   		 log.info(JSON.toJSONString(save));
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}
	

}
